﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

namespace BookTransactionControllers.Connection
{
    public class Connect
    {
        public static SqlConnection GetConnect()
        {

            try
            {
                String conStr = @"Data Source=BABMAN92-PC\MSSQLSERVER1;Initial Catalog=QLSachCu;Persist Security Info=True;User ID=sa;Password=1234567";
                SqlConnection connect = new SqlConnection(conStr);
                return connect;
            }
            catch (System.Exception ex)
            {
                return null;
            }
        }

        public static DataTable ExcuteQuery(SqlCommand command, string tblName)
        {
            try
            {
                SqlConnection connect = GetConnect();
                if (connect.State == ConnectionState.Open)
                {
                    connect.Close();
                }
                connect.Open();
                command.Connection = connect;
                SqlDataAdapter adapter = new SqlDataAdapter(command);
                DataTable table = new DataTable(tblName);
                adapter.Fill(table);
                return table;
            }
            catch (System.Exception)
            {
                return null;
            }
        }

        public static bool ExcuteNonQuery(SqlCommand command)
        {
            try
            {
                SqlConnection connect = GetConnect();
                if (connect.State == ConnectionState.Open)
                {
                    connect.Close();
                }
                connect.Open();
                command.Connection = connect;
                command.ExecuteNonQuery();
                command.Dispose();
                connect.Close();
                return true;
            }
            catch (System.Exception ex)
            {
                return false;
            }
        }
    }
}
